package com.aplink.generic.cloudmessage;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.WakefulBroadcastReceiver;

import com.aplink.generic.GenericActivity;
import com.aplink.generic.cache.MasterConfigManager;
import com.aplink.generic.google.maps.ui.LocationUtil;
import com.aplink.generic.pojo.CloudNotification;
import com.aplink.generic.pojo.MasterConfig;
import com.aplink.generic.sharepreference.GenericSharePreference;
import com.aplink.generic.webservice.GenericWebService;
import com.google.android.gms.gcm.GoogleCloudMessaging;

public class GCMIntentService extends IntentService implements
		LocationUtil.CallBack {

	private CloudNotification cloudNotification = null;

	private MasterConfig masterConfig;
	private NotificationManager mNotificationManager;

	public GCMIntentService() {
		super("GCMIntentService");
	}

	@Override
	public void handleLocationChange(Location location) {
		if (location.getAccuracy() <= 15) {
			LocationUtil.getInstance().disconnect();
			LocationUtil.getInstance().dispose();
			GenericWebService.newInstance().updateLocation(location).excute();
		}
	}

	@Override
	protected void onHandleIntent(final Intent intent) {
		if (masterConfig == null) {
			masterConfig = MasterConfigManager.getInstance().get(
					GenericSharePreference.getInstance(this)
							.getMasterConfigFileID());
			this.cloudNotification = masterConfig.getCloudNotification();
		}
		final Bundle extras = intent.getExtras();
		final GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
		final String messageType = gcm.getMessageType(intent);
		if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
			sendNotification(extras.getString(this.cloudNotification
					.getMessageTag()));
		}
		WakefulBroadcastReceiver.completeWakefulIntent(intent);
	}

	private void sendNotification(final String msg) {
		// Uri uri = Uri.parse(getTableContentURI() + "/CloudMessage");
		// ContentValues contentValues = new ContentValues();
		// contentValues.put("CloudMessage", msg);
		// contentValues.put("ReceiveDateTime", new Date().toString());
		// getContentResolver().insert(uri, contentValues);

		if (msg.equalsIgnoreCase("Request your location")) {
			if (!LocationUtil.getInstance().isConnected()) {
				LocationUtil.getInstance()
						.getDefaultLocationService(this, getBaseContext())
						.connect();
			}
		} else {
			this.mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
			Intent intent = new Intent(this, GenericActivity.class);
			final PendingIntent contentIntent = PendingIntent.getActivity(this,
					0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
			final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
					this)
					.setAutoCancel(true)
					.setContentTitle(this.cloudNotification.getContentTitle())
					.setSmallIcon(this.cloudNotification.getIconID())
					.setDefaults(
							Notification.DEFAULT_LIGHTS
									| Notification.DEFAULT_SOUND)
					.setContentText(msg);
			mBuilder.setContentIntent(contentIntent);
			this.mNotificationManager.notify(msg.hashCode(), mBuilder.build());
		}
	}
}
